#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
int ansnum;
int anscut[32];
int temcut[32];
struct Nod
{
    int f[11];
    int ci, ti;
    int id;
    bool operator <(const Nod &a) const
    {
        return ti < a.ti;
    }
} cut[32];
int used[210];
int W, M, N;
int estimate(int u)
{
    int num = 0;
    int x = u;
    for (int i = u + 1; i <= N; i++)
    {
        if (cut[i].ti - cut[x].ti <= W && cut[i].ci >= 3)
        {
            x = i;w
            num += 1;
        }
    }
    return num;
}
void dfs(int u, int fa, int len)
{
    for (int i = u; i <= N; i++)
    {
        int ci = cut[i].ci;
        int xx = estimate(i);
        if ((cut[i].ti - cut[fa].ti <= W || fa == 0) && len + xx + 1 > ansnum)
        {
            int num = 0;
            for (int j = 0; j < ci; j++)
                if (!used[cut[i].f[j]])
                    num++;
            if (num >= 3)
            {
                for (int j = 0; j < ci; j++)
                    used[cut[i].f[j]]++;
                temcut[len + 1] = cut[i].id;
                dfs(i + 1, u, len + 1);
                for (int j = 0; j < ci; j++)
                    used[cut[i].f[j]]--;
            }
        }
        else
            continue;
    }
    if (len > ansnum)
    {
        for (int i = 1; i <= len; i++)
            anscut[i] = temcut[i];
        ansnum = len;
    }
}
int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d%d", &N, &M, &W);
        for (int i = 1; i <= N; i++)
        {
            scanf("%d%d", &cut[i].ci, &cut[i].ti);
            cut[i].id = i;
            for (int j = 0; j < cut[i].ci; j++)
            {
                scanf("%d", &(cut[i].f[j]));
            }
        }
        cut[0].ti = 0;
        sort(cut + 1, cut + N + 1);
        ansnum = 0;
        memset(used,0,sizeof(used));
        for (int i = 1; i <= N; i++)
            dfs(i, 0, 0);
        printf("%d\n", ansnum);
        if (ansnum == 0)
            continue;
        sort(anscut + 1, anscut + ansnum + 1);
        printf("%d", anscut[1]);
        for (int i = 2; i <= ansnum; i++)
            printf(" %d", anscut[i]);
        printf("\n");
    }
    return 0;
}
